package com.j2eefast.produce.pms.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.*;
import com.j2eefast.produce.pms.mapper.PmsTestProjectMapper;
import com.j2eefast.produce.pms.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>委托单</p>
 *
 * @author: summer
 * @date: 2021-07-08 22:03
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/pms/entrust")
public class PmsEntrustController extends BaseController {
	private String urlPrefix = "modules/pms/entrust";
	@Autowired
	private PmsEntrustService pmsEntrustService;
	@Autowired
	private PmsChargeStandardListService pmsChargeStandardListService;
	@Autowired
	private PmsConfigureListService pmsConfigureListService;

	@Resource
	protected PmsTestProjectService pmsTestProjectService;

	@RequiresPermissions("pms:entrust:view")
	@GetMapping()
	public String process() {
		return urlPrefix + "/index";
	}

	@RequestMapping("/list")
	@RequiresPermissions("pms:entrust:list")
	@ResponseBody
	public ResponseData list(@RequestParam Map<String, Object> params) {
		PageUtil page = pmsEntrustService.findPage(params);
		return success(page);
	}

	@RequestMapping("/getEntrustList")
	@RequiresPermissions("pms:entrust:getEntrustList")
	@ResponseBody
	public ResponseData getEntrustList(@RequestParam Map<String, Object> params) {
		HashMap map = new HashMap<String, Object>();
		List<PmsTestProjectEntity> testProject1=new ArrayList<PmsTestProjectEntity>();
		List<PmsTestProjectEntity> testProject2=new ArrayList<PmsTestProjectEntity>();
//		List<String> testProject3=new ArrayList<>();
		List<PmsChargeStandardListEntity> testProject3=new ArrayList<PmsChargeStandardListEntity>();
		List<PmsChargeStandardListEntity> pmsChargeStandardListEntity=pmsChargeStandardListService.getEntrustList(params);

		for(int i=0;i<pmsChargeStandardListEntity.size()-1;i++){
//			System.out.println("33333");
//			System.out.println(pmsChargeStandardListEntity.get(i).getTestingProject()!=pmsChargeStandardListEntity.get(i+1).getTestingProject());
//			System.out.println("33333");
			if (pmsChargeStandardListEntity.get(i).getTestingProject()!=pmsChargeStandardListEntity.get(i+1).getTestingProject()) {
//				testProject3.add(pmsChargeStandardListEntity.get(i).getTestingProject());
				testProject3.add(pmsChargeStandardListEntity.get(i+1));
			}
		}
		List<PmsTestProjectEntity> testProjectEntity=pmsTestProjectService.getsProjectList(params);

		List<PmsConfigureListEntity> configureList=pmsConfigureListService.getConfigureList(params);
		for(int i=0;i<testProjectEntity.size();i++){
			switch (testProjectEntity.get(i).getTestType()){
				case 1 :
					testProject1.add(testProjectEntity.get(i));
					break;
				case 2:
					testProject2.add(testProjectEntity.get(i));
					break;
			}
//			if(testProjectEntity.get(i).getTestType()==1){
//				testProject1.add(testProjectEntity.get(i));
//			}
//			if(testProjectEntity.get(i).getTestType()==2){
//				testProject2.add(testProjectEntity.get(i));
//			}
		}

		map.put("standard", testProject3);
		map.put("product", testProject1);
		map.put("method", testProject2);
		map.put("configure", configureList.get(0));
		return success("成功",map);
	}

	/**
	 * 删除
	 */
	@BussinessLog(title = "删除", businessType = BusinessType.DELETE)
	@RequestMapping( value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("pms:entrust:del")
	@RequiresRoles(Constant.SU_ADMIN)
	@ResponseBody
	public ResponseData delete(Long[] ids) {
		return pmsEntrustService.deleteByIds(ids)?success(): error("删除失败!");
	}


	/**
	 * 修改出入库 页面
	 */
	@RequiresPermissions("pms:entrust:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		PmsEntrustEntity pmsEntrustEntity = pmsEntrustService.getById(id);
		mmap.put("entrust", pmsEntrustEntity);
		return urlPrefix + "/edit";
	}

	/**
	 * 修改出入库保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:entrust:edit")
	@BussinessLog(title = "修改", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData edit(PmsEntrustEntity pmsEntrustEntity) {
		ValidatorUtil.validateEntity(pmsEntrustEntity);
		return pmsEntrustService.updateById(pmsEntrustEntity) ? success() : error("修改失败!");
	}

	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("pms:entrust:add")
	@GetMapping("/add")
	public String add(ModelMap mmap) {
		Map<String,Object> map=pmsEntrustService.codes();
		System.out.println("1111111111");
		System.out.println(map.get("title"));
		System.out.println("22222222222");
		mmap.put("title",map.get("title"));
		mmap.put("total",map.get("total"));
		return urlPrefix + "/entrustAdd";

	}



	/**
	 * 新增保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:entrust:add")
	@BussinessLog(title = "认领", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData addSave(@Validated PmsEntrustEntity pmsEntrustEntity) {
		//校验参数
		ValidatorUtil.validateEntity(pmsEntrustEntity);
		return pmsEntrustService.saves(pmsEntrustEntity) ? success() : error("新增失败!");
	}

//	/**
//	 * 分配任务
//	 */
//	@GetMapping("/taskAdd/{id}")
//	public String task(@PathVariable("id") Long id, ModelMap mmap){
//		//mmap.put("comp",  busClassifyService.findCompById(id));
//		mmap.put("task",  id);
//		return urlPrefix + "/task";
//	}

//	/**
//	 * 列表保存
//	 */
//	@RepeatSubmit
//	@RequiresPermissions("pms:entry:taskAdd")
//	@BussinessLog(title = "列表保存", businessType = BusinessType.INSERT)
//	@RequestMapping(value = "/taskAdd", method = RequestMethod.POST)
//	@ResponseBody
//	public ResponseData taskAdd(@Validated PmsEntrustEntity pmsEntrustEntity) {
//		//校验参数
//		ValidatorUtil.validateEntity(pmsEntrustEntity);
////		String  createBy= UserUtils.getUserInfo().getName();
////		Date data=new Date();
////		pmsEntrustEntity.setUpdateBy(createBy);
////		pmsEntrustEntity.setUpdateTime(data);
//		return pmsEntrustService.updateById(pmsEntrustEntity) ? success() : error("新增失败!");
//	}





}
